package cn.stylefeng.guns.modular.business.frequency.modifyrecord.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.stylefeng.guns.core.exception.ServiceException;
import cn.stylefeng.guns.core.factory.PageFactory;
import cn.stylefeng.guns.core.pojo.page.PageResult;
import cn.stylefeng.guns.modular.business.frequency.modifyrecord.entity.ModifyRecord;
import cn.stylefeng.guns.modular.business.frequency.modifyrecord.mapper.ModifyRecordMapper;
import cn.stylefeng.guns.modular.business.frequency.modifyrecord.model.param.ModifyRecordParam;
import cn.stylefeng.guns.modular.business.frequency.modifyrecord.service.ModifyRecordService;
import cn.stylefeng.guns.modular.business.frequency.modifyrecord.enums.ModifyRecordExceptionEnum;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * 修改记录 服务实现类
 *
 * @author yujie
 * @date 2023/05/09 17:17
 */
@Service
public class ModifyRecordServiceImpl extends ServiceImpl<ModifyRecordMapper, ModifyRecord> implements ModifyRecordService {

    @Override
    public PageResult<ModifyRecord> page(ModifyRecordParam modifyRecordParam) {

        // 构造条件
        LambdaQueryWrapper<ModifyRecord> queryWrapper = new LambdaQueryWrapper<>();

        // 查询分页结果
        return new PageResult<>(this.page(PageFactory.defaultPage(), queryWrapper));
    }

    @Override
    public List<ModifyRecord> list(ModifyRecordParam modifyRecordParam) {

        // 构造条件
        LambdaQueryWrapper<ModifyRecord> queryWrapper = new LambdaQueryWrapper<>();

        return this.list(queryWrapper);
    }

    @Override
    public void add(ModifyRecordParam modifyRecordParam) {

        // 将dto转为实体
        ModifyRecord modifyRecord = new ModifyRecord();
        BeanUtil.copyProperties(modifyRecordParam, modifyRecord);

        this.save(modifyRecord);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void delete(ModifyRecordParam modifyRecordParam) {
        this.removeById(modifyRecordParam.getId());
    }

    @Override
    public void edit(ModifyRecordParam modifyRecordParam) {

        // 根据id查询实体
        ModifyRecord modifyRecord = this.queryModifyRecord(modifyRecordParam);

        // 请求参数转化为实体
        BeanUtil.copyProperties(modifyRecordParam, modifyRecord);

        this.updateById(modifyRecord);
    }

    @Override
    public ModifyRecord detail(ModifyRecordParam modifyRecordParam) {
        return this.queryModifyRecord(modifyRecordParam);
    }



    /**
     * 获取修改记录
     *
     * @author yujie
     * @since 2023/05/09 17:17
     */
    private ModifyRecord queryModifyRecord(ModifyRecordParam modifyRecordParam) {
        ModifyRecord modifyRecord = this.getById(modifyRecordParam.getId());
        if (ObjectUtil.isEmpty(modifyRecord)) {
            throw new ServiceException(ModifyRecordExceptionEnum.NOT_EXISTED);
        }
        return modifyRecord;
    }
}